<?php
namespace QnuiRings\Model;
// 软件名称：微折页面组件
// 代码日期：v2.0 2018-11-27
// 作者：杨道泉 
// 文件名：PageComponentsModel.class.php
// 功能：读取页面模块及其设置
class PageComponentsModel{
	public $page_id;
	public $sid;
	public function getComponets(){
		$CMPS = M("PageComponentsInpage");
		$components = $CMPS->where(Array(
						"sid"=>$this->sid,
						"page_id"=>$this->page_id
						))
					 ->order("ordinal ASC")
					 ->getField("id,name,mix,code,ordinal,model_private,status");
		
		$SETS = M("PageComponentSettingsInpage");
		$settings = $SETS->where(Array(
							"sid"=>$this->sid,
							"page_id"=>$this->page_id
							))
						 ->getField("id,component_inpage_id,field_name,name,val");
		foreach($settings as $id=>$set){
			if($set["name"]=="value"){
				$form_value[$set["component_inpage_id"]][$set["field_name"]]=$set["val"];
			}else{
				$form_field[$set["component_inpage_id"]][$set["field_name"]][$set["name"]]=$set["val"];
			}
		}
		foreach($components as $id=>$com){
			$components[$id]["formField"]=$form_field[$id];
			$components[$id]["formData"]=$form_value[$id];
			$components[$id]["isDelete"]=($com["status"]=="delete");
			$components[$id]["model_private"]=($com["model_private"]==1);
		}
		return array_values($components);
	}
	public function getComponetsSettings(){
		$CMPS = M("PageComponentsInpage");
		$components = $CMPS->where(Array(
						"sid"=>$this->sid,
						"page_id"=>$this->page_id
						))
					 ->order("ordinal ASC")
					 ->getField("id,mix,code");
					 
		$SETS = M("PageComponentSettingsInpage");
		$settings = $SETS->where(Array("sid"=>$this->sid,
						   "page_id"=>$this->page_id,
						   "name"=>"value",
						   "val"=>array('neq',"title")
						   ))
						 ->getField("id,component_inpage_id,field_name,val");
		foreach($components as $component_inpage_id=>$comp){
			foreach($settings as $set){
				if($component_inpage_id==$set["component_inpage_id"]){
					$components[$component_inpage_id][$set["field_name"]]=$set["val"];
					
					if(strpos($comp["code"],"countdown_prom")>=0 && $set["field_name"]=="countdownId"){
						//倒计时折扣读取倒计时分钟数
						$TAG_TASK = M("TagTask");
						$countdown = $TAG_TASK->where(Array("id"=>$set["val"]))->getField("countdown");
						$components[$component_inpage_id]["countdownNum"]=$countdown*60;//转换成秒
					}
				}
				
			}
		}
		return array_values($components);
	}
	
}